<?php

#[静态文件控制器]
class FilesController extends CommonController
{
    protected $authExcept = ['upload'];


    #[上传文件]
    public function uploadAction()
    {
        $inputs = $this->postData;
        Validate::check($inputs, [
            'token'     => ['role' => 'required'],
            'file_path' => ['role' => 'required'],
            'file_body' => ['role' => 'required'],
        ]);
        if(md5($inputs['file_path'] . ENCRYPT_IV)!==$inputs['token']){
            ret(30002, 'token无效');
        }
        $dirpath = pathinfo($inputs['file_path'], PATHINFO_DIRNAME);
        if ($dirpath !== '.' && !file_exists($dirpath)) {
            mkdir($dirpath, 0777, true);
        }
        if (!file_exists($inputs['file_path'])){
            // 重组文件内容
            file_put_contents($inputs['file_path'], base64_decode($inputs['file_body']));
        }
        ret(0, '文件同步成功');
    }

}
